package logic

import (
	"context"
	"fmt"
	"house/models/mysql"
	"time"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResAddLogic {
	return &ResAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResAddLogic) ResAdd(in *houserpc.ResAddRequest) (*houserpc.ResAddResponse, error) {
	// todo: add your logic here and delete this line
	parse, _ := time.Parse(time.DateTime, in.ResTime)
	add := parse.Add(-time.Hour * 8)
	var ress mysql.Res
	mysql.DB.Debug().Where("res_time = ?", in.ResTime).Where("house_id = ?", in.HouseId).Find(&ress)
	if ress.ID != 0 {
		return nil, fmt.Errorf("该房源在所选时间段已被他人预约")
	}
	var res = mysql.Res{
		UserId:  int(in.UserId),
		HouseId: int(in.HouseId),
		ResTime: add,
		Statue:  in.Statue,
	}
	mysql.DB.Debug().Create(&res)
	return &houserpc.ResAddResponse{RedId: int64(res.ID)}, nil
}
